package com.twu.biblioteca;

/**
 * Created with IntelliJ IDEA.
 * User: Joe
 * Date: 7/07/13
 * Time: 8:03 PM
 * To change this template use File | Settings | File Templates.
 */
public final class ParseAndExecute {

    public enum Selection {
        EXIT(0), VIEWBOOKS(1), VIEWMOVIES(2), RESERVEBOOK(3), CHECK(4) ;

        private int value;

        private Selection(int val) {
            value = val;
        }

        public int getValue() {
            return value;
        }
    }

    public static void Login (LibrarySystem librarySystem,LibraryIO libraryIO){

        String userID = "";
        String password = "";

        LibraryIO.printToDisplay("**If you don't have a log in or your login is invalid you will gain access to the public system only**");
        LibraryIO.printToDisplay("Please login:");
        LibraryIO.printToDisplay("UserID->");
        userID = libraryIO.ReadLine();
        LibraryIO.printToDisplay("Password->");
        password = libraryIO.ReadLine();

        try{
            librarySystem.setUser(User.getUser(userID, password));
        }
        catch (User.UserNotFoundException e) {
            LibraryIO.printToDisplay(e.getMessage());
        }
    }

    public static void Launch(LibrarySystem librarySystem,LibraryIO libraryIO){

        StringBuilder selection = new StringBuilder();
        LibraryIO.printToDisplay(LibraryMessages.WelcomeMessage);

        while(librarySystem.IsSystemActive)
        {
            selection.setLength(0);
            if (librarySystem.IsUserLoggedIn())
               LibraryIO.printToDisplay(LibraryMessages.UserMenuDisplay);
            else
                LibraryIO.printToDisplay(LibraryMessages.PublicMenuDisplay);

            LibraryIO.printToDisplay("Enter Choice->");
            selection.append(libraryIO.ReadLine());

            try{
                ParseAndExecute.ProcessAndExecuteSelection(librarySystem, libraryIO, Integer.parseInt(selection.toString()));
            }
            catch(NumberFormatException e){
                InvalidSelection();
            }
        }
    }

    public static void ProcessAndExecuteSelection(LibrarySystem librarySystem, LibraryIO libraryIO, int selection){

        if (selection == Selection.EXIT.getValue())
            ExitSystem(librarySystem);
        else if (selection== Selection.VIEWBOOKS.getValue())
            ViewBooks(librarySystem);
        else if (selection== Selection.VIEWMOVIES.getValue())
            ViewMovies(librarySystem);
        else if (selection == Selection.RESERVEBOOK.getValue())
            ReserveBook(librarySystem, libraryIO);
        else if (selection == Selection.CHECK.getValue())
            CheckDetails(librarySystem);
        else
            InvalidSelection();
    }

    private static void InvalidSelection() {
        LibraryIO.printToDisplay(LibraryMessages.SystemMessages.INVALIDSELECTION.getMessage());
    }

    public static void ReserveBook(LibrarySystem librarySystem, LibraryIO libraryIO){

        if(librarySystem.IsUserLoggedIn()){
            System.out.print("Please enter the book ID ->");
            int bookId = Integer.parseInt(libraryIO.ReadLine());

            if(Reservation.isItemAvailable(librarySystem.getReservations(), bookId, Reservation.ReservationType.BOOK)){

                if(librarySystem.addReservation(new Reservation(bookId, librarySystem.getLoggedInUser().name, Reservation.ReservationType.BOOK))){
                    LibraryIO.printToDisplay(LibraryMessages.SystemMessages.RESERVED.getMessage());
                }
            }
            else
                LibraryIO.printToDisplay(LibraryMessages.SystemMessages.NOTAVAILABLE.getMessage());
        }
        else
            LibraryIO.printToDisplay(LibraryMessages.SystemMessages.INVALIDSELECTION.getMessage());
    }

    public static void ExitSystem(LibrarySystem librarySystem)
    {
        LibraryIO.printToDisplay("Goodbye.....");
        librarySystem.IsSystemActive = false;
    }

    public static void ViewBooks(LibrarySystem librarySystem)
    {
        LibraryIO.printToDisplay("List of all Books...\n");
        LibraryIO.printToDisplay(librarySystem.ListAllBooks());
    }

    public static void ViewMovies(LibrarySystem librarySystem)
    {
        LibraryIO.printToDisplay("List of all Books...\n");
        LibraryIO.printToDisplay(librarySystem.ListAllMovies());
    }

    public static void CheckDetails(LibrarySystem librarySystem)
    {
        if(librarySystem.IsUserLoggedIn())
            LibraryIO.printToDisplay(librarySystem.getLoggedInUser().toString());
        else
            LibraryIO.printToDisplay(LibraryMessages.SystemMessages.SEEKASSISTANCE.getMessage());
    }

}
